package com.suzhiliang.springbootlesson;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Druid监控器类
 * 开启监控功能，可以在应用运行的过程中，
 * 通过监控提供的多维度数据来分析使用数据库的运行情况，
 * 从而可以调整程序设计，以便于优化数据库的访问性能。
 */
@Configuration
public class DruidConfiguration {

    //实现Druid的访问Servlet
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //设置ip白名单
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        //设置ip黑名单，如果allow和deny共同存在allow优于deny
        servletRegistrationBean.addInitParameter("deny", "192.168.8.100");
        //设置控制台用户
        servletRegistrationBean.addInitParameter("loginUsername", "druid");
        servletRegistrationBean.addInitParameter("loginPassword", "123");
        //是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }

    //实现Druid的访问Filter
    @Bean
    public FilterRegistrationBean statFilter() {
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //设置过滤器路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤形式
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
